package com.photobook.base.view.activity.impl;

import roboguice.activity.RoboFragmentActivity;
import roboguice.event.Observes;
import roboguice.inject.InjectView;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.google.inject.Inject;
import com.photobook.R;
import com.photobook.base.event.FacebookEvent;
import com.photobook.base.presenter.IFacebookInitPresenter;

/**
 * The Class MainActivity.
 */
public class MainActivity extends RoboFragmentActivity implements
		OnClickListener {

	/** The Constant BTN_FACEBOOK. */
	private static final int BTN_FACEBOOK = R.id.btn_auth_facebook;

	/** The facebook bo. */
	@Inject
	private IFacebookInitPresenter facebookInitPresenter;

	/** The btn facebook. */
	@InjectView(BTN_FACEBOOK)
	private Button btnFacebook;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.support.v7.app.ActionBarActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		init();
	}

	/**
	 * Init the.
	 */
	private void init() {
		this.facebookInitPresenter.initFacebook(this);
		btnFacebook.setOnClickListener(this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(final Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	/**
	 * Start facebook.
	 */
	private void startFacebook() {
		if (facebookInitPresenter.isFacebookLoggedIn()) {
			facebookInitPresenter.logoutFacebook();
		} else {
			facebookInitPresenter.doAuthenticationFacebook();
		}
	}

	/**
	 * Change ui facebook.
	 */
	private void changeUIFacebook() {
		if (facebookInitPresenter.isFacebookLoggedIn()) {
			btnFacebook.setText("Sair Facebook");
		} else {
			btnFacebook.setText("Entrar Facebook");
		}
	}

	/**
	 * Done facebook operation.
	 * 
	 * @param event
	 *            the event
	 */
	protected void doneFacebookOperation(@Observes final FacebookEvent event) {
		changeUIFacebook();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(final View v) {
		switch (v.getId()) {
		case BTN_FACEBOOK:
			startFacebook();
			break;

		default:
			break;
		}

	}

}
